///<reference path="../../../core/model/CommonDto.ts"/>
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { FormValidatorService } from "../../../core/service/form-validator.service";
import { HttpService } from "../../../core/service/http.service";
import { Register } from "../../../core/model/Register";
import { HttpRes } from "../../../core/model/HttpRes";
import { Router, ActivatedRoute } from "@angular/router";
import { AuthService } from "../../../core/service/auth.service";
import { NzMessageService, NzModalRef, NzModalService } from 'ng-zorro-antd';
import { Login } from "../../../core/model/Login";
import { ClassInfoOutput } from "../../../core/model/ClassInfoDto";
import { PagerSearchInput, PagedResultDto, ElementViewType } from "../../../core/model/CommonDto";


@Component({
  selector: 'app-my-drill-edit',
  templateUrl: './my-drill-edit.component.html',
  styleUrls: ['../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss', './my-drill-edit.component.scss']
})
export class StudentDrillEditComponent implements OnInit {
  drillId;
  _drillsubject;
  _drillCourse;
  subjectsData;
  _drillCourseSubject;
  typeoptions = [
    { dValue: 0, caption: '自由演练' },
    { dValue: 1, caption: '角色演练' },
    { dValue: 2, caption: '分组演练' }
  ];
  drillType = 0;
  creatorRole;
  drillRoles;
  IsShowdrillRoles = false;
  resultMsg;
  oldDrillRecord;
  isfirst = true;
  get drillCourseSubject() {
    return this._drillCourseSubject;
  }

  set drillCourseSubject(value) {
    this._drillCourseSubject = value;
    this.subjectsData.forEach(e => {
      if (e.keystr === value) {
        this._drillsubject = e.subjectId;
        this._drillCourse = e.courseId;
      }
    });

    if (this.isfirst === false) {
       this.getDrillRoles();
      } 
  }
  signForm: FormGroup;
  constructor(public fb: FormBuilder,
    public FormValidator: FormValidatorService,
    private modal: NzModalService,
    public http: HttpService,
    public router: Router,
    public route: ActivatedRoute,
    public Auth: AuthService) {
    this.newMethod();
  }

  private newMethod() {
    this.drillId = this.route.snapshot.params['id'];
    this.GetMySubject();
  }

  ngOnInit() {
  }

  async GetMySubject() {
    this.subjectsData = new Array();
    const subjects = await this.http.post<any>('Course/GetCourseSubjects', "");
    if (subjects.length !== 0) {
      this.subjectsData = subjects;

      // this.drillsubject=this.subjectsData[0].dValue;
      const thisdrill = await this.http.get<any>('DrillRecord/GetRecordById?drillRecordId=' + parseInt(this.drillId, 10));
      this.oldDrillRecord = thisdrill;
      this.drillCourseSubject = thisdrill.courseSubject;

      this._drillsubject = thisdrill.subjectId;
      this._drillCourse = thisdrill.courseId;

      this.drillType = thisdrill.drillType;
      this.getDrillRolesIni();
      this.isfirst = false;
    }
  }

  async getDrillRolesIni() {
    const roles = await this.http.get<any>('DrillRecord/GetDrillRoles?drillRecordId=' + parseInt(this._drillsubject, 10));
    this.drillRoles = new Array();
    this.drillRoles = roles;
    if (roles.length !== 0) {
      this.drillRoles = roles;
      if (this.drillType === 0) {
        this.creatorRole = this.drillRoles[0].dValue;
      } else {
        this.creatorRole = this.oldDrillRecord.creatorRoleId.toString();
      }
    }
  }
  async getDrillRoles() {
    const roles = await this.http.get<any>('DrillRecord/GetDrillRoles?drillRecordId=' + parseInt(this._drillsubject, 10));
    this.drillRoles = new Array();
    this.drillRoles = roles;
    if (roles.length !== 0) {
      this.drillRoles = roles;
      this.creatorRole = this.drillRoles[0].dValue;
    } else {
      this.creatorRole = null;
    }
  }

  async save() {
    if (this._drillsubject == null || this._drillCourse == null) {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '演练主题不能为空。'
      });
      return;
    }
    if (this.drillType == null) {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '演练类型不能为空。'
      });
      return;
    }
    if (this.drillType === 0) {
      const par = { 
        Id: this.drillId, DrillSubjectId: this._drillsubject, DrillType: this.drillType, CreatorRoleId: null, CourseId: this._drillCourse };
      this.resultMsg = await this.http.post<any>('DrillRecord/UpdateDrillRecord', par);
    } else {
      if (this.creatorRole == null) {
        this.modal.warning({
          nzTitle: '提示',
          nzContent: '角色不能为空。'
        });
        return;
      }
      const par = { 
        Id: this.drillId, DrillSubjectId: this._drillsubject, DrillType: this.drillType, 
        CreatorRoleId: this.creatorRole, CourseId: this._drillCourse };
      this.resultMsg = await this.http.post<any>('DrillRecord/UpdateDrillRecord', par);
    }

    // 判断是否成功
    switch (this.drillType) {
      case 0: this.router.navigate(['app/mydrill']); break;
      case 1: this.router.navigate(['app/mydrill']); break;
      case 2: this.router.navigate(['app/mydrill']); break;
      default:
    }
  }

  backpage() {
    window.history.go(-1);
  }
}

